﻿using System.IO;

namespace MC2Development.DevWebServer.Helpers
{
    /// <summary>
    /// Extensions to the Stream class used by the Web Server
    /// </summary>
    public static class StreamExtensions
    {
        /// <summary>
        /// Writes the content of one stream to another
        /// </summary>
        /// <param name="source">The source stream</param>
        /// <param name="target">The target stream</param>
        public static void WriteTo(this Stream source, Stream target)
        {
            WriteTo(source, target, 1024);
        }

        /// <summary>
        /// Writes the content of one stream to another with a specified buffer size
        /// </summary>
        /// <param name="source">The source stream</param>
        /// <param name="target">The target stream</param>
        /// <param name="bufferLength">The buffer size to use</param>
        public static void WriteTo(this Stream source, Stream target, int bufferLength)
        {
            byte[] buffer = new byte[bufferLength];
            int bytesRead = 0;

            do
            {
                bytesRead = source.Read(buffer, 0, buffer.Length);
                target.Write(buffer, 0, bytesRead);
            } while (bytesRead > 0);
        }
    }
}
